/*
用户权限
*/
import { routerRedux } from 'dva/router';
import api from '../api';
import { productType,origProductType } from '../config/dict';

export default {
	namespace:'accoutuserper',
	state:{
		pdtlist:[],
		searchName:''
	},
	subscriptions:{
		setup({dispatch,history}) {
			history.listen(({pathname,query}) => {
				if( pathname === `/systemMgt/accountMgt/userMgt/permission` ){
					dispatch({
						type: 'getUserpdtList',
						payload: {
							...query,
						}
					})
				}
			})
		}
	},
	effects:{
		*changeRoute({payload:query},{call,put}){
			yield put(routerRedux.push({
				pathname:`/systemMgt/accountMgt/userMgt/permission`,
				query:{
					...query,
				}
			}))
		},
		*changeState({payload:query},{call,put}){
			yield put({
				type:"initData",
				payload:query
			})
		},
		* getUserpdtList({ payload:query }, { call, put }) {
			yield put({
				type:"initData",
				payload:{
					pdtlist:[]
				}
			})
			const response = yield call(api.getUserpdt, query);
			yield put({
				type: 'getListSuccess',
				payload: response ,
			});
		},
		* saveUsrsPermission({ payload }, { call, put }) {
			const username = payload.username
			const response = yield call(api.saveUserPermission, payload.username,payload.data);
			yield put({
				type: 'saveSuccess',
				payload: { response },
			});
			// yield put(routerRedux.goBack());
			yield put({
				type:'getUserpdtList',
				payload:{username:payload.username}
			})
		},
	},
	reducers:{
		initData(state,{payload}){
			return{
				...state,
				...payload
			}
		},
		getListSuccess(state, {payload}) {
			const {data,page,userProducts} = payload
			data.forEach((item) => {
				item.productTypeText = productType[item.productType];
				item.origProductTypeText = origProductType[item.origProductType] || item.origProductType;;
			});
			const {checklist} = state

			const status=data ? true:false
			return {
				...state,
				status,
				pdtlist:data,
				page,
				userProducts
			}
		},
		saveSuccess(state, action) {
			const { payload: { response } } = action;
			return {
				...state,
			};
		},
	}


}
